package health.lm.com.component_base.popup.upgrade;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.xiaochen.progressroundbutton.AnimButtonLayout;
import com.xiaochen.progressroundbutton.AnimDownloadProgressButton;

import health.lm.com.component_base.R;
import health.lm.com.component_base.popup.model.PopupModel;
import health.lm.com.component_base.popup.uplevel.UpLevelPopUtil;
import health.lm.com.component_base.widget.CustomPopWindow;
import health.lm.com.network.callback.SimpleCallBack;
import io.reactivex.Observable;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by Android Studio .
 * 作者：zhong
 * 日期：2018/12/21
 * 邮箱：15206394364@163.com
 * 介绍：
 * 修订：====================
 */
public class UpgradeProgressUtil{
    private static UpgradeProgressUtil progressUtil;
    TextView mTvTitle;
    TextView mTvSecondTitle;
    AnimButtonLayout mAblProgress;
    TextView mTvContent;
    TextView mTvSecondContent;
    TextView mBtnOk;
    TextView mBtnToInvite;
    private Context context;
    private CustomPopWindow mPopWindow;
    private View contentView;
    private View rootView;
    private OnClick onClick;
    private String link_url;

    private UpgradeProgressUtil() {
    }

    public static UpgradeProgressUtil getInstance() {
        if (progressUtil == null) progressUtil = new UpgradeProgressUtil();
        return progressUtil;
    }

    public UpgradeProgressUtil init(Context context, View rootView) {
        this.context = context;
        this.rootView = rootView;
        contentView = LayoutInflater.from(context).inflate(R.layout.pop_upgrade_progress, null);
//        mPopWindow = new CustomPopWindow.PopupWindowBuilder(context)
//                .setView(contentView)
//                .enableBackgroundDark(true)
//                .setBgDarkAlpha(0.7f)
//                .setAnimationStyle(R.style.pop_bottom_anim)
//                .size(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
//                .create()
//                .showAtLocation(rootView, Gravity.CENTER, 0, 0);

        mTvTitle = contentView.findViewById(R.id.tv_title);
        mTvSecondTitle = contentView.findViewById(R.id.tv_second_title);
        mAblProgress = contentView.findViewById(R.id.abl_progress);
        mAblProgress.setState(AnimDownloadProgressButton.DOWNLOADING);
        mTvContent = contentView.findViewById(R.id.tv_content);
        mTvSecondContent = contentView.findViewById(R.id.tv_second_content);
        mBtnOk = contentView.findViewById(R.id.btn_ok);
        mBtnToInvite = contentView.findViewById(R.id.tv_to_invite);
        mBtnOk.setOnClickListener(v-> mPopWindow.dissmiss());
        mBtnToInvite.setOnClickListener(v->{
            //ToastUtils.showShort("跳转邀请海报");
            if (onClick != null) onClick.onCall(link_url);
            mPopWindow.dissmiss();
        });

//        mTvTitle.setText("首次达成2500代币 +200");
//        mTvSecondTitle.setText("已达成1900/2500");
//        mTvContent.setText("领取下一个奖励，大约需要邀请三名好友，连续签到或邀请高质量好友，可以帮你更快速达成目标");
//        mTvSecondContent.setText("领取下一个奖励，大约需要邀请三名好友，连续签到或邀请高质量好友，可以帮你更快速达成目标");
//        mAblProgress.setProgressText("", 70);

        mBtnOk.setText("我知道了");
        mBtnToInvite.setText("去邀请");
        return this;
    }

    @SuppressLint("CheckResult")
    public UpgradeProgressUtil loadProgress(OnClick onClick) {
        this.onClick = onClick;
        PopupModel.getInstance().levelUp(new SimpleCallBack<ThinkTankLevelUpEntity>() {
            @Override
            public void onSuccess(ThinkTankLevelUpEntity pThinkTankLevelUpEntity) {
                link_url = pThinkTankLevelUpEntity.getLink_url();
                show();
                mTvTitle.setText(pThinkTankLevelUpEntity.getTitle());
                mTvSecondTitle.setText(pThinkTankLevelUpEntity.getAlready());
                mTvContent.setText(pThinkTankLevelUpEntity.getStr1());
                mTvSecondContent.setText(pThinkTankLevelUpEntity.getStr2());
                //mAblProgress.setProgressText("", entity.getPercent());
                //若满级，则提示升级
                if (pThinkTankLevelUpEntity.getPercent() == 100) {
                    UpLevelPopUtil.getInstance().init(context, contentView);
                    return;
                }

                Observable.create((ObservableOnSubscribe<Integer>) emitter -> {
                    for (int a = 0; a <= pThinkTankLevelUpEntity.getPercent(); a++) {
                        try {
                            Thread.sleep(20);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        emitter.onNext(a);
                    }
                }).subscribeOn(Schedulers.newThread())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(integer -> mAblProgress.setProgressText("", integer));
            }
        });
        return this;
    }

    public UpgradeProgressUtil show() {
        mPopWindow = new CustomPopWindow.PopupWindowBuilder(context)
                .setView(contentView)
                .enableBackgroundDark(true)
                .setBgDarkAlpha(0.7f)
                .setAnimationStyle(R.style.pop_bottom_anim)
                .size(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
                .create()
                .showAtLocation(rootView, Gravity.CENTER, 0, 0);
        return this;
    }

    public interface OnClick {
        void onCall(String url);
    }
}
